﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MeterServiceClientApp.UC;
using EntityAndDomainLibrary.DomainModel;
using MeterServiceClientApp.UC.Config;
using Log4NetLibrary;

namespace MeterServiceClientApp
{
    public partial class frmMeterPooling : Form
    {
        #region Private Variables
        DataBaseAccess dbaccess = new DataBaseAccess();
        IList<UserControl> _listUC = new List<UserControl>();
        ILogService logService; 
        #endregion 

        public frmMeterPooling()
        {
            try
            {
                log4net.Config.XmlConfigurator.Configure();
                logService = new FileLogService(typeof(frmMeterPooling));
                logService.Error("Client Application Started");
                InitializeComponent();
                LoadLatestMeterDataUC();
            }

            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }
        }

        #region Events
        private void item_Click(object sender, EventArgs e)
        {
            //ToolStripItem item = (ToolStripItem)sender;
            //ucAllMeterDataByMeterName uc = new ucAllMeterDataByMeterName(Convert.ToInt32(item.Tag));

            ////this.pnlMain.Controls.Clear();
            //ClearUC();
            //pnlMain.Controls.Add(uc);

            //pnlMain.Dock = DockStyle.Fill;
            //this.Refresh();

            //uc.Location = new Point(uc.Location.X, uc.Location.Y + 30);
            //_listUC.Add(uc);
        }

        private void allMetersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ClearUC();
                LoadLatestMeterDataUC();
            }

            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }
        }

        private void dailyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                //ClearUC();
                //ucTrendingReport uc = new ucTrendingReport();

                //this.pnlMain.Controls.Add(uc);
                //_listUC.Add(uc);

                //uc.Location = new Point(uc.Location.X, uc.Location.Y + 30);
                frmReport frm = new frmReport();
                frm.Show();
            }

            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }
        }

        private void metersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ClearUC();
                ucMeterPtConfig uc = new ucMeterPtConfig();

                this.pnlMain.Controls.Add(uc);
                uc.Location = new Point(uc.Location.X, uc.Location.Y + 30);

                _listUC.Add(uc);
            }

            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }
        }

        private void systemSettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ClearUC();
                ucSystemSettingsConfig uc = new ucSystemSettingsConfig();

                this.pnlMain.Controls.Add(uc);
                uc.Location = new Point(uc.Location.X, uc.Location.Y + 30);

                _listUC.Add(uc);
            }

            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }
        }

        private void singleMetersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ucAllMeterDataByMeterName uc = new ucAllMeterDataByMeterName();

                ClearUC();
                pnlMain.Controls.Add(uc);

                pnlMain.Dock = DockStyle.Fill;
                this.Refresh();

                uc.Location = new Point(uc.Location.X, uc.Location.Y + 30);
                _listUC.Add(uc);
            }

            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }

        } 
        #endregion

        #region Helpers
        private void ClearUC()
        {
            try
            {
                foreach (UserControl uc in _listUC)
                {
                    pnlMain.Controls.Remove(uc);
                }

                _listUC.Clear();
            }

            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }
        }

        private void LoadLatestMeterDataUC()
        {
            try
            {
                ucAllMetersLatestUpdate uc = new ucAllMetersLatestUpdate();
                pnlMain.Controls.Add(uc);

                this.Refresh();
                uc.Location = new Point(uc.Location.X, uc.Location.Y + 40);

                _listUC.Add(uc);
            }

            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }
        }

        private void LoadMetersDataToToolstrip()
        {
            try
            {
                IList<Meterpoint> _listmeterpts = dbaccess.GetAllMeterpoint();

                foreach (Meterpoint mp in _listmeterpts)
                {
                    ToolStripItem item = new ToolStripMenuItem();
                    item.Text = mp.name;

                    item.Tag = mp.meterpointid;
                    item.Click += new EventHandler(item_Click);
                    singleMetersToolStripMenuItem.DropDownItems.Add(item);
                }
            }

            catch (Exception ex)
            {
                logService.Error(ex.Message);
            }
        } 
        #endregion
    }
}
